var ns, app;
app = new Vue({
    el: '#app',
    data: {
        form: {
            // "id": 1, 
            // "customerName": "李文静", 
            // "customerPhone": "18711111111", 
            // "OrderStatus": 1, 
            // "OrderStatusDesc": "派送中", 
            // "status": 5, 
            // "statusDesc": "送货上门",
            //  "fromAddress": "社区配送站-北大门收货点",
            //   "toAddress": "东岸国际-蔷薇馆-1栋1单元-109", 
            //   "appointTime": "今日18:00-18:30", 
            //   "goodWeight": "4.79", 
            //   "isTimingOut": '即将超时', 
            //   "remark": "都是买的水果，请轻拿轻放，谢谢",
            //    orderItemList: []
        },
        ID: 0,
        IsShowMore: false,
    },
    methods: {
        get_data: function () {
            var that = this;
            var options = {};
            options.id = that.ID;
            options.action = "getdeliverydetail";
            ns.post(options, function (succeed, data, err) {
                console.log(JSON.stringify(data));
                if (succeed) {
                    that.form = data;

                } else if (err) {
                    api.toast({
                        msg: err,
                        duration: 2000,
                        location: 'bottom'
                    });
                }
            }, {
                toast: true
            });
        },
        tellCallPhone: function (PnoneNumber) {
            if (!PnoneNumber) {
                api.toast({
                    msg: '联系电话为空',
                    duration: 2000,
                    location: 'bottom'
                });
                return;
            }
            api.confirm({
                title: '提示',
                msg: '确定拨打电话',
                buttons: ['确定', '取消']
            }, function (ret, err) {
                if (ret) {
                    if (ret.buttonIndex == 1) {
                        api.call({
                            type: 'tel_prompt',
                            number: PnoneNumber
                        });
                    };
                };
            });
        },
        //接单
        doOrderReceiveSave: function () {
            var that = this;
            api.confirm({
                title: '提示',
                msg: '确认接单?',
                buttons: ['确认接单', '取消']
            }, function (ret, err) {
                if (ret.buttonIndex == 1) {
                    ns.post({
                        action: 'acceptdelivery',
                        id: that.ID,
                    }, function (succeed, data, err) {
                        if (succeed) {
                            api.toast({
                                msg: '操作成功',
                                duration: 2000,
                                location: 'bottom'
                            });
                            api.sendEvent({
                                name: 'do_load_data',
                            });
                        } else if (err) {
                            api.toast({
                                msg: err,
                                duration: 2000,
                                location: 'bottom'
                            });
                        }
                    });
                }
            });
        },
        //配送完成
        doOrderService: function (type) {
            var that = this;
            ns.openWin('distribution_send_ok', '配送完成', {
                id: that.ID,
            });
            // api.confirm({
            //     title: '提示',
            //     msg: '确认配送完成?',
            //     buttons: ['确认', '取消']
            // }, function (ret, err) {
            //     if (ret.buttonIndex == 1) {
            //         ns.post({
            //             action: 'confirmdelivery',
            //             id: that.ID,
            //         }, function (succeed, data, err) {
            //             if (succeed) {
            //                 api.toast({
            //                     msg: '操作成功',
            //                     duration: 2000,
            //                     location: 'bottom'
            //                 });
            //                 api.sendEvent({
            //                     name: 'do_load_data',
            //                 });
            //             } else if (err) {
            //                 api.toast({
            //                     msg: err,
            //                     duration: 2000,
            //                     location: 'bottom'
            //                 });
            //             }
            //         });
            //     }
            // });

        },
        doOrderCheck: function (type) {
            var title = '验收取货';
            if (type == 1) {
                title = '配送单退回';
            }
            var that = this;
            ns.openWin('distribution_exam', title, {
                ID: that.ID,
                IsReject: type
            });
        },
        do_viewimages: function (activeIndex) {
            var that = this;
            var imageUrls = [];
            for (var i = 0; i < that.form.orderItemList.length; i++) {
                imageUrls.push(that.form.orderItemList[i].itemCoverImg);
            }
            var imageBrowser = api.require('imageBrowser');
            imageBrowser.openImages({
                showList: false,
                activeIndex: activeIndex,
                imageUrls: imageUrls
            });
        },
        doshowmore: function () {
            var that = this;
            that.IsShowMore = !that.IsShowMore;
        },
    }
});
apiready = function () {
    api.parseTapmode();
    ns = window.Foresight.Util;
    app.ID = api.pageParam.ID;
    app.get_data();

    api.addEventListener({
        name: 'do_load_data'
    }, function (ret, err) {
        app.get_data();
    });
};
